package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.dao.MarketKeywordMapper;
import com.cskaoyan.catmall.dao.MarketSearchHistoryMapper;
import com.cskaoyan.catmall.model.pojo.MarketUser;
import com.cskaoyan.catmall.model.vo.HistoryKeywordWxVO;
import com.cskaoyan.catmall.model.vo.KeywordWxVO;
import com.cskaoyan.catmall.model.vo.SearchHelperWx;
import com.cskaoyan.catmall.model.vo.SearchIndexWxVO;
import com.cskaoyan.catmall.service.SearchServiceWx;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @date: 2022-01-11-09-24
 * @author: ZYY
 * @description:
 */
@Service
public class SearchServiceWxImp implements SearchServiceWx {

    @Autowired
    MarketKeywordMapper keywordMapper;
    @Autowired
    MarketSearchHistoryMapper searchHistoryMapper;

    @Autowired
    MarketSearchHistoryMapper historyMapper;

    @Override
    public SearchIndexWxVO searchIndex() {
        KeywordWxVO defaultKeyWord= keywordMapper.selectLimit();
        List<KeywordWxVO> hotKeywordList = keywordMapper.selectByHot();
        // todo Shiro安装框架完成以后获得UserId
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        int userId = user.getId();
        List<HistoryKeywordWxVO> historyKeywordList = searchHistoryMapper.selectByUserId(userId);

        SearchIndexWxVO data = new SearchIndexWxVO();
        data.setDefaultKeyword(defaultKeyWord);
        data.setHotKeywordList(hotKeywordList);
        data.setHistoryKeywordList(historyKeywordList);
        return data;
    }

    @Override
    public SearchHelperWx searchHelper(String keyword) {
        SearchHelperWx data =new SearchHelperWx();
        String [] keywords = keywordMapper.select( keyword);
        data.setKeyword(keywords);
        return data;
    }

    @Override
    public int clearHistory() {
        //todo 1 根据userId逻辑删除
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        int userId = user.getId();
        int i = historyMapper.update(userId);
        return i;
    }
}